<template>
  <div>
    <el-table :data="cartProductions" style="width: 100%">
      <el-table-column prop="id" label="商品ID" width="180"></el-table-column>
      <el-table-column prop="name" label="商品名称" width="180"></el-table-column>
      <el-table-column label="数量" width="180">
        <template slot-scope="scope">
          <el-input-number
            :min="1"
            size="mini"
            :value="scope.row.num"
            @input="handleBlur"
            @change="handleChange( scope.row )"
          ></el-input-number>
        </template>
      </el-table-column>
      <el-table-column prop="price" label="单价" width="180"></el-table-column>
      <el-table-column prop="totalPrice" label="总价" width="180"></el-table-column>
      <el-table-column label="操作" width="180">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="danger"
            icon="el-icon-delete"
            plain
            @click="dialogVisibleTrue( scope.row )"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <Info v-if="total_num"></Info>

    <el-dialog title="注意" :visible.sync="dialogVisible" width="20%">
      <span>确定要删除这个商品吗？</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="dialogSure">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters, mapActions } from "vuex"
import Info from "./info.vue"
export default {
  name: "cart",
  data () {
    return {
      dialogVisible: false,
      result: null,
      input_number_value: 1
    }
  },
  components: {
    Info
  },
  computed: {
    ...mapGetters(["cartProductions", "total_num"])
  },
  methods: {
    ...mapActions(["numChange", "delProduct"]),
    dialogVisibleTrue (data) {
      this.dialogVisible = true
      this.result = data
      console.log(this.result)
    },
    dialogSure () {
      this.delProduct(this.result)
      this.dialogVisible = false
    },
    handleBlur (value) {
      this.input_number_value = value
    },
    handleChange (data) {
      data.value = this.input_number_value
      this.numChange(data);
    }
  }
};
</script>

<style scoped>
.el-table th > .cell {
  text-align: center;
}
.el-table td > .cell {
  text-align: center;
}
</style>
